/*
 * Created on Feb 9, 2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.broadvision.manage.common.tools.action.common;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import com.broadvision.data.client.Category;
import com.broadvision.data.client.DataManager;
import com.broadvision.data.common.CallSettings;
import com.broadvision.data.common.ContentUID;
import com.broadvision.data.exceptions.DataException;

/**
 * @author kwlee
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class CategoryPickerPopupAction extends CategorizedCntPickerPopupAction {
  
  public static String CATEGORY_BROWSE_PATH = "mt.categoryOnly.browse.path";
  public static String CATEGORY_SEARCH_PATH = "mt.categoryOnly.search.path";

  /* (non-Javadoc)
   * @see com.broadvision.manage.common.tools.action.common.BasePickerPopupAction#getList(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  public List getList(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws Exception {
    return new ArrayList();
  }

  /* (non-Javadoc)
   * @see com.broadvision.manage.tools.action.BaseAction#processExecute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  protected ActionForward processExecute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws Exception {
      
    DynaActionForm pickerPopupForm = (DynaActionForm)form;
    if (pickerPopupForm == null || mapping == null) {
      request.setAttribute(Globals.ERROR_KEY, "error.application");
      return mapping.findForward("failure");
    }// if

/*** set "categoryOnly" to true */
    PropertyUtils.setSimpleProperty(pickerPopupForm, 
                                    "categoryOnly", 
                                    new Boolean(true));
                                      
/*** set "skipItemsList" to true */
    PropertyUtils.setSimpleProperty(pickerPopupForm, 
                                    "skipItemsList", 
                                    new Boolean(true));

                                      
    return super.processExecute(mapping, form, request, response);
  }

  /* (non-Javadoc)
   * @see com.broadvision.manage.common.tools.action.common.BasePickerPopupAction#getTabPageMap()
   */
  protected String[][] getTabPageMap(HttpServletRequest request) {
    String[][] tabPageMap = {{getMessage(request, BROWSE), getMessage(request, CATEGORY_BROWSE_PATH), "browseTab"},
                             {getMessage(request, SEARCH), getMessage(request, CATEGORY_SEARCH_PATH), "searchTab"}};
    return tabPageMap;
  }

  /* (non-Javadoc)
   * @see com.broadvision.manage.common.tools.action.common.BasePickerPopupAction#getItemsNameList(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.util.List)
   */
  protected List getItemsNameList(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response,
    List itemsIdList)
    throws Exception {

    // Get the content type id
    DynaActionForm pickerPopupForm = (DynaActionForm)form;
    int cntTypeId = Integer.parseInt((String)PropertyUtils.getSimpleProperty(pickerPopupForm, "contentTypeId"));
    String contentTypeName = DataManager.getContentTypeName(cntTypeId);
                                        
    DataManager dm = new DataManager();
    CallSettings callsettings = dm.getCallSettings();
    callsettings.setFilteringFlag(false);

    // Create a list which contains a list of display name
    List returnList = new ArrayList();
    for (int i = 0; i < itemsIdList.size(); i++) {
    
      ContentUID catUid = new ContentUID(getServiceId(request), 
                                          ((Long)itemsIdList.get(i)).intValue(), 
                                          contentTypeName);
      if (dm.categoryExists(catUid) == true) {
        Category cat = dm.getCategory(catUid);
        returnList.add(cat.getPathName());
      }// if
    }// for

    return returnList;
  }

  /* (non-Javadoc)
   * @see com.broadvision.manage.common.tools.action.common.BasePickerPopupAction#getColumnName(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  public String getColumnName(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws Exception {
    return NAME;
  }
  
  protected boolean isValidId(int serviceId, int contentTypeId, long id) {
    boolean result = false;
    try {
      DataManager dm = new DataManager();
      CallSettings callsettings = dm.getCallSettings();
      callsettings.setFilteringFlag(false);
      String contentTypeName = DataManager.getContentTypeName(contentTypeId);
      ContentUID uid = new ContentUID(serviceId, id, contentTypeName);
      result = dm.categoryExists(uid);
    }// try
    catch (DataException ex) {
    }// catch
    return result;
  }
}
